package com.lunwen.view;

import com.lunwen.dao.ArticleDao;
import com.lunwen.domain.Article;
import com.lunwen.utils.StringUtils;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

public class DeleteArticleFrame extends Context implements ActionListener, KeyListener {
    private JTextField search = new JTextField(10);

    private JTextField sid = new JTextField(10);
    private JTextField article_name = new JTextField(10);
    private JTextField article_type = new JTextField(10);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private JButton mod = new JButton("确认删除");

    private ArticleDao dao = new ArticleDao();

    private List<Article> cache = null;
    private int indexOfCache = 0;
    private int aid = 0;

    public DeleteArticleFrame(int width, int height) {
        super(width, height);

        search.setBounds(60,20,width-120,40);
        this.add(search,"查找",100);
        search.setFocusable(true);
        search.addKeyListener(this);

        sid.setBounds(60,100,width-120,40);
        article_name.setBounds(60,150,width-120,40);
        article_type.setBounds(60,200,width-120,40);
        this.add(sid,"作者学号",100);
        this.add(article_name,"论文题目",100);
        this.add(article_type,"论文类型",100);


        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        mod.setBounds(60,380,width-120,48);
        this.add(mod,20);

        left.addActionListener(this);
        right.addActionListener(this);
        mod.addActionListener(this);
        
    }

    public void setValue(Article article, String id){
        sid.setText(id);
        article_name.setText(article.getName());
        article_type.setText(article.getType());
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        if (e.getKeyChar() == KeyEvent.VK_ENTER){
            cache = dao.find(search.getText());
            indexOfCache = 0;
            if (cache != null && cache.size() != 0){
                Article article = cache.get(indexOfCache);
                String sid = dao.findSidByBid(article.getAid());
                setValue(article,sid);
                aid = article.getAid();
                MassageTip.showMassage(null,"找到"+cache.size()+"个");
            }else {
                MassageTip.showMassage(null,"没有找到");
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    Article article = cache.get(indexOfCache);
                    String sid = dao.findSidByBid(article.getAid());
                    setValue(article,sid);
                    aid = article.getAid();
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    Article article = cache.get(indexOfCache);
                    String sid = dao.findSidByBid(article.getAid());
                    setValue(article,sid);
                    aid = article.getAid();
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }

        if (e.getSource() == mod){

            if (!StringUtils.isEmpty(sid.getText())) {

                int delete = dao.delete(aid);
                if (delete > 0){
                    MassageTip.showMassage(null,"删除成功");
                }else {
                    MassageTip.showMassage(null,"删除失败");
                }

            }
        }
    }
}
